/*
**    Copyright (C) 2003-2011 Institute for Systems Biology
**                            Seattle, Washington, USA.
**
**    This library is free software; you can redistribute it and/or
**    modify it under the terms of the GNU Lesser General Public
**    License as published by the Free Software Foundation; either
**    version 3 of the License, or (at your option) any later version.
**
**    This library is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**    Lesser General Public License for more details.
**
**    You should have received a copy of the GNU Lesser General Public
**    License along with this library; if not, write to the Free Software
**    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/


package org.systemsbiology.rface.hadoop.reducers;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Reducer;
import org.systemsbiology.hadoop.ioformats.LexicalKey;
import org.systemsbiology.rface.hadoop.ConfigConstants;
import org.systemsbiology.rface.hadoop.data.TargetRecord;

public class AnalyzeTargetConsistency extends Reducer<LexicalKey, TargetRecord, LexicalKey, TargetRecord.InconsistencyResult>{
	private int head, pessimism;
	protected void setup(Reducer<LexicalKey, TargetRecord, LexicalKey, TargetRecord.InconsistencyResult>.Context context)
			throws IOException, InterruptedException{
		Configuration config = context.getConfiguration();
		head = Integer.parseInt(config.get(ConfigConstants.LIST_APATHY_THRESHOLD));
		pessimism = Integer.parseInt(config.get(ConfigConstants.MISSING_PREDICTOR_PESSIMISM));
	}
	protected void reduce(LexicalKey key, Iterable<TargetRecord> values, Context context)
			throws IOException, InterruptedException{
		TargetRecord squishie = new TargetRecord();
		for(TargetRecord t : values){
			squishie.mergeIn(t);
		}
		context.write(key, squishie.inconsistencyScore(head, pessimism));
	}
}
